<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>考试成绩查询与分析系统</title>
    <!-- Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    <!-- Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- Chart.js -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.8/dist/chart.umd.min.js"></script>
    <!-- 配置Tailwind -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#3b82f6',
                        secondary: '#10b981',
                        accent: '#8b5cf6',
                        neutral: '#64748b',
                        success: '#22c55e',
                        warning: '#f59e0b',
                        danger: '#ef4444',
                    },
                    fontFamily: {
                        sans: ['Inter', 'system-ui', 'sans-serif'],
                    },
                },
            }
        }
    </script>
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .nav-shadow {
                box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            }
            .card-hover {
                transition: all 0.3s ease;
            }
            .card-hover:hover {
                transform: translateY(-5px);
                box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
            }
            .gradient-bg {
                background: linear-gradient(135deg, #3b82f6 0%, #8b5cf6 100%);
            }
        }
    </style>
</head>
<body class="bg-gray-50 font-sans">
    <!-- 固定导航栏 - 使用模板 -->
    {% include '_navbar.html' %}

    <!-- 主要内容区域 -->
    <main class="container mx-auto px-4 pt-28 pb-16">
        <!-- 欢迎区域 -->
        <section class="mb-12">
            <div class="bg-white rounded-xl shadow-sm p-8">
                <div class="flex flex-col md:flex-row justify-between items-center">
                    <div>
                        <h1 class="text-3xl font-bold text-gray-800 mb-2">欢迎使用考试成绩管理系统</h1>
                        <p class="text-gray-500">你好，{{ current_user.username }}！查看最新的考试项目和成绩信息。</p>
                    </div>
                    {% if current_user.role == 'admin' %}
                    <div class="mt-6 md:mt-0">
                        <a href="/batch_query" class="px-6 py-3 bg-primary text-white rounded-lg hover:bg-primary/90 transition-colors duration-200 flex items-center space-x-2">
                            <i class="fa fa-users"></i>
                            <span>批量查询成绩</span>
                        </a>
                    </div>
                    {% endif %}
                </div>
            </div>
        </section>

        <!-- 考试项目区域 -->
        <section class="mb-16">
            <div class="flex justify-between items-center mb-6">
                <div>
                    <h2 class="text-2xl font-bold text-gray-800">考试项目</h2>
                    <p class="text-gray-500">查看所有考试项目及其统计信息</p>
                </div>
            </div>

            <!-- 考试项目卡片 -->
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                {% if projects %}
                    {% for project in projects %}
                    <div class="bg-white rounded-xl shadow-sm p-6 card-hover">
                        <div class="flex justify-between items-start mb-4">
                            <h3 class="text-xl font-bold text-gray-800">{{ project.name }}</h3>
                            <div class="bg-blue-100 text-primary px-3 py-1 rounded-full text-sm">
                                {{ project.student_count }} 名学生
                            </div>
                        </div>
                        
                        <div class="space-y-4">
                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <p class="text-sm text-gray-500">语文平均分</p>
                                    <p class="text-lg font-semibold text-gray-800">{{ project.chinese_average }}</p>
                                </div>
                                <div>
                                    <p class="text-sm text-gray-500">语文最高分</p>
                                    <p class="text-lg font-semibold text-gray-800">{{ project.chinese_top }}</p>
                                </div>
                            </div>
                            
                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <p class="text-sm text-gray-500">数学平均分</p>
                                    <p class="text-lg font-semibold text-gray-800">{{ project.maths_average }}</p>
                                </div>
                                <div>
                                    <p class="text-sm text-gray-500">数学最高分</p>
                                    <p class="text-lg font-semibold text-gray-800">{{ project.maths_top }}</p>
                                </div>
                            </div>
                            
                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <p class="text-sm text-gray-500">英语平均分</p>
                                    <p class="text-lg font-semibold text-gray-800">{{ project.english_average }}</p>
                                </div>
                                <div>
                                    <p class="text-sm text-gray-500">英语最高分</p>
                                    <p class="text-lg font-semibold text-gray-800">{{ project.english_top }}</p>
                                </div>
                            </div>
                        </div>
                        
                        <div class="mt-6">
                            <button class="w-full py-2 border border-primary text-primary rounded-lg hover:bg-primary/5 transition-colors duration-200 flex items-center justify-center">
                                <i class="fa fa-bar-chart mr-2"></i>
                                查看详细统计
                            </button>
                        </div>
                    </div>
                    {% endfor %}
                {% else %}
                    <div class="col-span-full bg-gray-50 border border-dashed border-gray-300 rounded-xl p-12 text-center">
                        <i class="fa fa-calendar-o text-4xl text-gray-300 mb-4"></i>
                        <h3 class="text-lg font-medium text-gray-700 mb-2">暂无考试项目</h3>
                        <p class="text-gray-500">请添加新的考试项目以开始成绩管理</p>
                    </div>
                {% endif %}
            </div>
        </section>

        <!-- 最新成绩区域 -->
        <section class="mb-16">
            <div class="flex justify-between items-center mb-6">
                <div>
                    <h2 class="text-2xl font-bold text-gray-800">最新成绩</h2>
                    <p class="text-gray-500">查看最近的学生成绩信息</p>
                </div>
                <div class="flex space-x-2">
                    <button class="px-4 py-2 bg-gray-200 text-gray-700 rounded-md hover:bg-gray-300 transition-colors flex items-center space-x-1">
                        <i class="fa fa-filter"></i>
                        <span>筛选</span>
                    </button>
                    <button class="px-4 py-2 bg-gray-200 text-gray-700 rounded-md hover:bg-gray-300 transition-colors flex items-center space-x-1">
                        <i class="fa fa-download"></i>
                        <span>导出</span>
                    </button>
                </div>
            </div>

            <!-- 成绩表格 -->
            <div class="bg-white rounded-xl shadow-sm overflow-hidden">
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">考试项目</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">学号</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">姓名</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">语文</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">数学</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">英语</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">总分</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">班级排名</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            {% if latest_scores %}
                                {% for score in latest_scores %}
                                <tr>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-primary">{{ score.project_name }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ score.student_xuehao }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-800">{{ score.student_name }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ score.chinese_score }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ score.maths_score }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ score.english_score }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm font-semibold text-gray-800">{{ score.all_original_score or score.three_score }}</td>
                                    <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">{{ score.class_ranking }}</td>
                                </tr>
                                {% endfor %}
                            {% else %}
                                <tr>
                                    <td colspan="8" class="px-6 py-12 text-center text-gray-500">
                                        <div class="flex flex-col items-center">
                                            <i class="fa fa-graduation-cap text-4xl text-gray-300 mb-4"></i>
                                            <p>暂无成绩数据</p>
                                        </div>
                                    </td>
                                </tr>
                            {% endif %}
                        </tbody>
                    </table>
                </div>
                
                {% if latest_scores and latest_scores|length > 5 %}
                <div class="px-6 py-4 bg-white border-t border-gray-200 sm:px-6 flex items-center justify-center">
                    <a href="#" class="text-primary hover:text-primary/80 text-sm font-medium">查看更多成绩</a>
                </div>
                {% endif %}
            </div>
        </section>

        <!-- 统计图表区域 -->
        <section class="mb-16">
            <div class="mb-6">
                <h2 class="text-2xl font-bold text-gray-800">成绩统计分析</h2>
                <p class="text-gray-500">查看各科目成绩分布情况</p>
            </div>

            <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                <!-- 语数外平均分对比 -->
                <div class="bg-white rounded-xl shadow-sm p-6">
                    <h3 class="font-bold text-gray-800 mb-4">语数外平均分对比</h3>
                    <div class="h-64">
                        <canvas id="mainSubjectsChart"></canvas>
                    </div>
                </div>
                
                <!-- 物理化学生物平均分对比 -->
                <div class="bg-white rounded-xl shadow-sm p-6">
                    <h3 class="font-bold text-gray-800 mb-4">物理化学生物平均分对比</h3>
                    <div class="h-64">
                        <canvas id="scienceSubjectsChart"></canvas>
                    </div>
                </div>
            </div>
        </section>
    </main>

    <!-- 页脚 -->
    <footer class="bg-gray-800 text-white py-8">
        <div class="container mx-auto px-4">
            <div class="text-center">
                <p class="mb-2">考试成绩查询与分析系统</p>
                <p class="text-gray-400 text-sm">© 2024 版权所有</p>
            </div>
        </div>
    </footer>

    <!-- JavaScript -->
    <script>
        // 等待DOM加载完成
        document.addEventListener('DOMContentLoaded', function() {
            // 初始化图表
            initCharts();
        });

        // 初始化图表函数
        function initCharts() {
            // 语数外平均分对比图表
            const mainCtx = document.getElementById('mainSubjectsChart').getContext('2d');
            const mainChart = new Chart(mainCtx, {
                type: 'bar',
                data: {
                    labels: ['语文', '数学', '英语'],
                    datasets: [
                        {% if projects %}
                            {% for project in projects[:3] %}
                            {
                                label: '{{ project.name }}',
                                data: [{{ project.chinese_average }}, {{ project.maths_average }}, {{ project.english_average }}],
                                backgroundColor: 'rgba(59, 130, 246, ' + {{ 0.4 + loop.index * 0.2 }} + ')',
                                borderColor: 'rgba(59, 130, 246, 1)',
                                borderWidth: 1
                            }{% if not loop.last %},{% endif %}
                            {% endfor %}
                        {% else %}
                        {
                            label: '平均分',
                            data: [85, 82, 88],
                            backgroundColor: 'rgba(59, 130, 246, 0.6)',
                            borderColor: 'rgba(59, 130, 246, 1)',
                            borderWidth: 1
                        }
                        {% endif %}
                    ]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    scales: {
                        y: {
                            beginAtZero: true,
                            max: 100
                        }
                    }
                }
            });

            // 物理化学生物平均分对比图表
            const scienceCtx = document.getElementById('scienceSubjectsChart').getContext('2d');
            const scienceChart = new Chart(scienceCtx, {
                type: 'bar',
                data: {
                    labels: ['物理', '化学', '生物'],
                    datasets: [
                        {% if projects %}
                            {% for project in projects[:3] %}
                            {
                                label: '{{ project.name }}',
                                data: [{{ project.physics_average }}, {{ project.chemistry_average }}, {{ project.biology_average }}],
                                backgroundColor: 'rgba(139, 92, 246, ' + {{ 0.4 + loop.index * 0.2 }} + ')',
                                borderColor: 'rgba(139, 92, 246, 1)',
                                borderWidth: 1
                            }{% if not loop.last %},{% endif %}
                            {% endfor %}
                        {% else %}
                        {
                            label: '平均分',
                            data: [78, 75, 80],
                            backgroundColor: 'rgba(139, 92, 246, 0.6)',
                            borderColor: 'rgba(139, 92, 246, 1)',
                            borderWidth: 1
                        }
                        {% endif %}
                    ]
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    scales: {
                        y: {
                            beginAtZero: true,
                            max: 100
                        }
                    }
                }
            });
        }
    </script>
</body>
</html>